package at.fpmedv.eclipse.helma.core;

import helma.objectmodel.db.DbMapping;
import helma.objectmodel.db.Relation;

import java.util.Comparator;


public class CollectionTreeObject extends PropertyTreeObject {

	@Override
	public String getName() {
		Relation rel = getRelation();
		DbMapping targetType = rel.getTargetType();
		String accessName = "";
		String targetTypeName = "";
		if (rel.hasAccessName()) {
			accessName = ": " + rel.getAccessName();
		} else if (rel.getPrototype() == null && rel.getTargetType() != null){
			accessName = ": " + rel.getTargetType().getIDField();
		}
		if (targetType != null) {
			targetTypeName = "(" + targetType.getPrototypeName(null) + ")";
		}
		return super.getName() + targetTypeName + accessName;
	}
	
	public CollectionTreeObject(String name, Relation relation, ITreeParent parent) {
		super(name, relation, parent);
	}

	@Override
	public int compare(ITreeObject element, Comparator comparator) {
		// sort by name
		if (element instanceof CollectionTreeObject) {
			return comparator.compare(this.getName(), element.getName());
		}
		if (element instanceof MountPointTreeObject)
			return -1;
		if (element instanceof PropertyTreeObject)
			return 1;
		// greater than skin and functions
		return -1;
	}
	
	
}